
package com.hlkj.pay.controller.openapi;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.IMerchantAccessAppService;
import com.hlkj.pay.app.openapi.IOpenApiAppService;
import com.hlkj.pay.app.order.ISettleOrderAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.dto.merchant.MerchantAccessDto;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessInfoDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppChannelDO;
import com.hlkj.pay.service.merchant.IMerchantApplicationService;
import com.hlkj.pay.util.OpenApiParamsUtils;
import com.hlkj.pay.vo.openapi.merchant.req.access.OpenApiMerchantAccessQueryReq;
import com.hlkj.pay.vo.openapi.merchant.req.access.OpenApiMerchantAccessReq;
import com.hlkj.pay.vo.openapi.merchant.req.access.OpenApiMerchantAccessSettleQueryReq;
import com.hlkj.pay.vo.openapi.merchant.req.access.OpenApiOrderSettleSubmitReq;
import com.hlkj.pay.vo.openapi.merchant.resp.access.OpenApiMerchantAccessBalanceResp;
import com.hlkj.pay.vo.openapi.merchant.resp.access.OpenApiMerchantAccessResp;
import com.hlkj.pay.vo.openapi.merchant.resp.access.OpenApiOrderSettleResp;
import com.hlkj.pay.vo.order.OpenApiResult;
import com.hlkj.pay.vo.order.req.MerchantChannelSplitReq;
import com.hlkj.pay.vo.order.req.MerchantProductReq;
import com.hlkj.pay.vo.order.req.MerchantTermAddReq;
import com.hlkj.pay.vo.order.req.OrderCommonParamReq;
import com.hlkj.pay.vo.order.resp.MerchantProductQueryResp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/09 11:25
 */
@Tag(name = "进件-商户管理")
@RestController
@RequestMapping("/openapi/merchant")
@RequiredArgsConstructor
@Slf4j
public class OpenApiMerchantController {

    private final IOpenApiAppService openApiAppService;

    private final IMerchantAccessAppService merchantAccessAppService;

    private final ISettleOrderAppService merchantAccessAmountAppService;

    private final IMerchantApplicationService merchantApplicationService;

    @Operation(summary = "富友-终端报备")
    @PostMapping("/fuiou/term")
    OpenApiResult<String> termAdd(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            MerchantTermAddReq merchantTermAddReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), MerchantTermAddReq.class);
            CommonResult<String> stringOpenApiResult = merchantAccessAppService.fuiouTermAdd(merchantTermAddReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "商户信息录入")
    @PostMapping("/access/submit")
    OpenApiResult<OpenApiMerchantAccessResp> submitAccess(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiMerchantAccessReq apiMerchantAccessReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiMerchantAccessReq.class);
            CommonResult<MerchantAccessDto> stringOpenApiResult = merchantAccessAppService.submitAccess(apiMerchantAccessReq);
            if (stringOpenApiResult.isSuccess()) {
                OpenApiMerchantAccessResp openApiMerchantAccessResp = OpenApiParamsUtils.openApiMerchantAccessResp(stringOpenApiResult.getData());
                return openApiAppService.sign(CommonResult.success(openApiMerchantAccessResp));
            }
            return OpenApiResult.error(stringOpenApiResult.getCode(), stringOpenApiResult.getMsg());
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "申请单状态查询")
    @PostMapping("/access/detail")
    OpenApiResult<OpenApiMerchantAccessResp> accessDetail(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiMerchantAccessQueryReq accessQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiMerchantAccessQueryReq.class);
            CommonResult<MerchantAccessDto> stringOpenApiResult = merchantAccessAppService.accessDetail(accessQueryReq);
            if (stringOpenApiResult.isSuccess()) {
                OpenApiMerchantAccessResp openApiMerchantAccessResp = OpenApiParamsUtils.openApiMerchantAccessResp(stringOpenApiResult.getData());
                MerchantAccessInfoDO merchantAccessInfo = stringOpenApiResult.getData().getMerchantAccessInfo();
                if (MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS.getCode().equals(merchantAccessInfo.getStatus())) {
                    MerchantAppChannelDO merchantAppChannelDO = merchantApplicationService.queryDetailByAccessId(merchantAccessInfo.getId(),
                            MerchantEnum.DATA_RELATION_TYPE.ACCESS);
                    if (merchantAppChannelDO != null) {
                        openApiMerchantAccessResp.setMchChannelCode(merchantAppChannelDO.getCode());
                    }
                }
                return openApiAppService.sign(CommonResult.success(openApiMerchantAccessResp));
            }
            return OpenApiResult.error(stringOpenApiResult.getCode(), stringOpenApiResult.getMsg());
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "账户余额查询")
    @PostMapping("/access/balance")
    OpenApiResult<OpenApiMerchantAccessBalanceResp> accessBalance(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiMerchantAccessQueryReq accessQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiMerchantAccessQueryReq.class);
            CommonResult<OpenApiMerchantAccessBalanceResp> stringOpenApiResult = merchantAccessAmountAppService.accessBalance(accessQueryReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "账户余额提现")
    @PostMapping("/access/settle")
    OpenApiResult<OpenApiOrderSettleResp> accessSettle(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiOrderSettleSubmitReq accessQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiOrderSettleSubmitReq.class);
            CommonResult<OpenApiOrderSettleResp> stringOpenApiResult = merchantAccessAmountAppService.accessSettle(accessQueryReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "账户提现查询")
    @PostMapping("/access/settle/query")
    OpenApiResult<OpenApiOrderSettleResp> accessSettleQuery(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiMerchantAccessSettleQueryReq accessQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiMerchantAccessSettleQueryReq.class);
            CommonResult<OpenApiOrderSettleResp> stringOpenApiResult = merchantAccessAmountAppService.accessSettleQuery(accessQueryReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "AT重新报备")
    @PostMapping("/easy/split")
    OpenApiResult<String> easySplit(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            MerchantChannelSplitReq merchantChannelSplitReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), MerchantChannelSplitReq.class);
            CommonResult<MerchantChannelSplitReq> stringOpenApiResult = merchantAccessAppService.easySplit(merchantChannelSplitReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "AT重新报备")
    @PostMapping("/access/split")
    OpenApiResult<String> accessSplit(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            MerchantChannelSplitReq merchantChannelSplitReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), MerchantChannelSplitReq.class);
            CommonResult<MerchantChannelSplitReq> stringOpenApiResult = merchantAccessAppService.easySplit(merchantChannelSplitReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "产品变更")
    @PostMapping("/access/product/update")
    OpenApiResult<String> accessProductUpdate(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            MerchantProductReq merchantProductReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), MerchantProductReq.class);
            CommonResult<MerchantProductReq> stringOpenApiResult = merchantAccessAppService.updateProduct(merchantProductReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "产品变更状态查询")
    @PostMapping("/access/product/query")
    OpenApiResult<String> accessProductQuery(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            MerchantProductReq merchantProductReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), MerchantProductReq.class);
            CommonResult<MerchantProductQueryResp> stringOpenApiResult = merchantAccessAppService.queryProduct(merchantProductReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

}
